﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Rmlife.Data;
using Rmlife.Common;
using Rmlife.Config;
using Rmlife.Model;

namespace Rmlife.Site
{
    /// <summary>
    /// 用户控件基类
    /// </summary>
    public class UserControlBase : System.Web.UI.UserControl
    {
        #region 变量
        /// <summary>
        /// 当前用户的用户ID
        /// </summary>
        protected internal int userid;
        /// <summary>
        /// 当前用户的管理组ID
        /// </summary>
        protected internal int useradminid = 0;
        /// <summary>
        /// 当前用户名
        /// </summary>
        protected internal string username;
        /// <summary>
        /// 当前用户的用户组ID
        /// </summary>
        protected internal int usergroupid;
        /// <summary>
        /// 当前用户的用户组编号
        /// </summary>
        protected internal string accountgroupid;
        /// <summary>
        /// 组标题
        /// </summary>
        protected internal string grouptitle;
        /// <summary>
        /// 当前用户权限集
        /// </summary>
        /// <summary>
        /// 当前页面名称
        /// </summary>
        public string pagename = WebReq.GetPageName();
        /// <summary>
        /// 全局配置实例
        /// </summary>
        protected internal GeneralConfigInfo config;
        /// <summary>
        /// 当前用户的用户组信息
        /// </summary>

        #endregion

        public UserControlBase()
        {
            config = GeneralConfigs.GetConfig();
            userid = Utils.StrToInt(WebUtils.GetCookie("userid"), -1);
            // 获取用户信息
            OnlineUserInfo oluserinfo = new OnlineUserInfo();// OnlineUsers.UpdateInfo(config.Passwordkey, config.Onlinetimeout);
            userid = oluserinfo.Userid;
            usergroupid = oluserinfo.Groupid;
            username = oluserinfo.Username;

        
            if (useradminid > 0)
                accountgroupid = string.Empty;
        }

        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="action"></param>
        /// <param name="reson"></param>
    
        /// <summary>
        /// 检查当前用户是否是管理员
        /// </summary>
        /// <returns></returns>
        public bool CheckAdministrator()
        {
            return this.useradminid == 1;
        }
        /// <summary>
        /// 检查当前用户是否是普通用户权限
        /// </summary>
        /// <returns></returns>
        public bool CheckMyRole()
        {
            return usergroupid > -1 && useradminid == 0;
        }

        /// <summary>
        /// 检查当前用户是否有权限操作
        /// </summary>
        /// <returns></returns>
        public bool CheckRole(bool role)
        {
            return role || CheckAdministrator();
        }

    }
}
